package org.aersoft.jokebook.fragments;

import java.util.ArrayList;

import org.aersoft.jokebook.R;
import org.aersoft.jokebook.activities.ItemActivity;
import org.aersoft.jokebook.beans.ItemBean;
import org.aersoft.jokebook.core.Constants;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * Fragment class for items of {@link ItemBean} type.
 * @see {@link ItemActivity}
 */
public class ItemFragment extends Fragment {

	private int mPageNumber;
	private ArrayList<ItemBean> arrayBeans;

	/**
	 * Static builder of fragment items.
	 * @param pageNumber of {@link Integer} type.
	 * @param arrayBeans of {@link ArrayList} type.
	 * @return fragment of {@link ItemFragment} type.
	 */
	public static ItemFragment create(int pageNumber,
			ArrayList<ItemBean> arrayBeans) {
		ItemFragment fragment = new ItemFragment(arrayBeans);
		Bundle arguments = new Bundle();
		arguments.putInt(Constants.PAGE, pageNumber);
		fragment.setArguments(arguments);
		return fragment;
	}

	/**
	 * Constructor method of the fragment class.
	 * @param arrayBeans of {@link ArrayList} type.
	 */
	public ItemFragment(ArrayList<ItemBean> arrayBeans) {
		this.arrayBeans = arrayBeans;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mPageNumber = getArguments().getInt(Constants.PAGE);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(
				R.layout.fragment_item, container, false);

		TextView title = (TextView) rootView.findViewById(R.id.title);
		TextView content = (TextView) rootView.findViewById(R.id.content);
		TextView author = (TextView) rootView.findViewById(R.id.author);
		TextView date = (TextView) rootView.findViewById(R.id.date);
		Button prevButton = (Button) rootView.findViewById(R.id.prev);
		Button nextButton = (Button) rootView.findViewById(R.id.next);
		TextView pageNumber = (TextView) rootView.findViewById(R.id.pageNumber);

		ItemBean bean = arrayBeans.get(mPageNumber);
		title.setText(String.valueOf(bean.getTitle()));
		content.setText(String.valueOf(bean.getContent()));
		author.setText(bean.getAuthor());
		date.setText(bean.getDate());

		pageNumber.setText("Item " + String.valueOf(mPageNumber + 1) + " de "
				+ arrayBeans.size());
		nextButton.setText(String.valueOf(mPageNumber + 2));
		prevButton.setText(String.valueOf(mPageNumber));

		if (arrayBeans.size() < 2 || arrayBeans.size() == mPageNumber+1)
			nextButton.setEnabled(false);
		if (mPageNumber == 0)
			prevButton.setEnabled(false);
		return rootView;
	}

}
